package com.yusvn.scm.common.interceptor;

import com.jfinal.aop.Inject;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.yusvn.scm.admin.service.LoginService;
import com.yusvn.scm.common.constant.Const;
import com.yusvn.scm.common.kit.RetKit;
import com.yusvn.scm.common.model.SysUser;

/**
 * 登录拦截器
 * @author liugz
 *
 */
public class LoginInterceptor implements Interceptor  {

	@Inject
	LoginService loginSrv;
	
	@Override
	public void intercept(Invocation inv) {
		Controller ctl = inv.getController();
		String sessionId = ctl.getHeader(Const.TOKEN_HEADER_NAME);
		 System.out.println(" 拦截器LoginInterceptor的sessionId:"+sessionId);
		
		if (StrKit.isBlank(sessionId)) {
			ctl.renderJson(RetKit.fail(Const.CODE_NO_LOGIN, "未登录"));
			return;
		}
		
		SysUser loginUser = loginSrv.loginWithSessionId(sessionId, ctl.getRequest());
		if (loginUser == null) {
			ctl.renderJson(RetKit.fail(Const.CODE_NO_AUTH, "会话过期无效，请重新登录"));
			return;
		}
		
		//放行
		inv.invoke();
		
	}


}






